package com.stu.servlet;

import com.stu.dao.impl.StudentDaoImpl;
import com.stu.model.StudentAnswer;
import com.stu.model.User;
import com.stu.util.DaoUtile;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String action=req.getParameter("action");
        StudentDaoImpl studentDao=new StudentDaoImpl();
        User user=(User) req.getSession().getAttribute("session_newUser");
        String userAccount=user.getUserAccount();
        //点击选择老师的按钮或点击搜索老师的按钮
        if(action.equals("list")||action.equals("searchTeacher")){
            List<User> userList= null;
            if(action.equals("list")){
                try {
                    //调用获取老师信息的方法
                    userList= studentDao.getAllTeacher();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }else{
                //获取搜索的内容
                String searchText=req.getParameter("searchText");
                try {
                    //调用根据所搜内容查询用户信息的方法
                    userList=studentDao.getSearchTeacher(searchText);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
             //加载页面
            req.setAttribute("userList", userList);
            req.setAttribute("mainRight", "user.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
            //点击我的老师的按钮,点击删除我的老师的按钮
        } else if (action.equals("goMyTeacher")||action.equals("deleteMyTeacher")) {
            if(action.equals("deleteMyTeacher")){
                String teacherAccount=req.getParameter("teacherAccount");
                try {
                    //调用根据学生账户和老师账户的方法删除老师
                    studentDao.deleteMyTeacher(userAccount,teacherAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            List<User> userList= null;
            try {
                //根据学生账户查找自己的所有老师的方法
                userList = studentDao.goMyTeacher(userAccount);
                /*System.out.println(userList.get(0).getUserName());*/
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //加载页面
            req.setAttribute("userList", userList);
            req.setAttribute("mainRight", "myTeacher.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
            //点击选择老师的按钮
        } else if (action.equals("addTeacher")) {
            //获取所选老师的账户
            String teacherAccount=req.getParameter("teacherAccount");
            int flag= 0;
            try {
                //根据学生账户和老师账户查找该老湿是否已被学生选择
                flag = studentDao.isExistStuTea(userAccount,teacherAccount);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            if(flag==0){
                try {
                    studentDao.addTeacher(userAccount,teacherAccount);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                req.setAttribute("tip","添加成功");
            }else{
                req.setAttribute("tip","你已选择过该老师,本次选择失败");
            }
            req.setAttribute("mainRight", "blank.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
         //点击我的老师的按钮然后点击我的作业下的查看详情的按钮
        } else if (action.equals("goMyTeaTask")) {
            String teacherAccount=req.getParameter("teacherAccount");
            List<StudentAnswer> studentAnswerList= null;
            try {
                //根据老师的账户和学生的账户来获取学生的答题情况
                studentAnswerList = studentDao.goMyTeaTask(userAccount,teacherAccount);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //加载学生任务页面
            req.setAttribute("studentAnswerList", studentAnswerList);
            req.setAttribute("mainRight", "studentT.jsp");
            req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
               //点击我的老师->查看详情->点击答题
        }else if(action.equals("doAnswer")){
            String taskAccount=req.getParameter("taskAccount");
            req.setAttribute("taskAccount",taskAccount);
            //跳转答题页面
            req.getRequestDispatcher("doAnswer.jsp").forward(req, resp);
            //点击提交按钮
        }else if(action.equals("dealAnswer")){
            String taskAccount=req.getParameter("taskAccount");
            String answer=(req.getParameter("answer")).trim();
            try {
                //调用根据学生账户和任务账户以及答案的方法处理学生的答题
                studentDao.doAnswer(userAccount,taskAccount,answer);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //点击提交后就会加载success.jsp页面
            req.setAttribute("tip","恭喜答题完成,请关闭页面等待老师批改");
        req.getRequestDispatcher("success.jsp").forward(req, resp);

        }else if(action.equals("doScore")){
            String taskAccount=req.getParameter("taskAccount");
            String studentAccount=req.getParameter("studentAccount");
            req.setAttribute("taskAccount",taskAccount);
            req.setAttribute("studentAccount",studentAccount);
            req.getRequestDispatcher("getScore.jsp").forward(req, resp);
     //老师批改作业的动作即学生管理->查看任务详情->点击批改跳出一个网页弹窗
        }else if(action.equals("getScore")){
            String taskAccount=req.getParameter("taskAccount");
            String studentAccount=req.getParameter("studentAccount");
            String fenshu=req.getParameter("fenshu");
            try {
                //调用处理分数的方法
                studentDao.dealFenshu(taskAccount,studentAccount,fenshu);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            //根出提示信息
            req.setAttribute("tip","已修改分数");
            req.getRequestDispatcher("success.jsp").forward(req, resp);

        }

    }
}
